import { Button, Space } from 'ant-design-vue'
import { tableColumns } from './data'
import { takeFinishStatus } from '@/views/task/task-publish/data'

export function useColumns(updateFunction: Function, detailFunction: Function, finishFunction: Function) {
  const getColumns = (column, record) => {
    if (column.dataIndex === 'name') {
      return <span>{record.name}</span>
    } else if (column.dataIndex === 'address') {
      let result = ''
      if (record.address && record.address.length > 0) {
        for (let i = 0; i < record.address.length; i++) {
          result += record.address[i]
          if (i != record.address.length - 1) {
            result += ','
          }
        }
      }
      return <span>{result}</span>
    } else if (column.dataIndex === 'tagCodeList') {
      let result = ''
      if (record.tagCodeList && record.tagCodeList.length > 0) {
        for (let i = 0; i < record.tagCodeList.length; i++) {
          result += record.tagCodeList[i]
          if (i != record.tagCodeList.length - 1) {
            result += ','
          }
        }
      }
      return <span>{result}</span>
    } else if (column.dataIndex === 'status') {
      let takeFinishStatusStr = ''
      takeFinishStatus.forEach(model => {
        if (record.status == model.value) {
          takeFinishStatusStr = model.label
        }
      })
      return <span>{takeFinishStatusStr}</span>
    } else if (column.dataIndex === 'deadline') {
      return <span>{record.deadline}</span>
    } else if (column.dataIndex === 'specificTask') {
      return <span>{record.specificTask}</span>
    } else if (column.key === 'action') {
      return (
        <Space>
          <Button type='link' onClick={() => detailFunction(record.code)} v-auth={['base:shop:detail']}>
            详情
          </Button>
          <Button
            type='link'
            disabled={record.status === takeFinishStatus[2].value}
            onClick={() => updateFunction(record.code)}
            v-auth={['base:shop:edit']}
          >
            编辑
          </Button>
          <Button
            type='link'
            disabled={record.status != takeFinishStatus[0].value}
            onClick={() => finishFunction(record.code)}
            v-auth={['base:shop:edit']}
          >
            完成
          </Button>
        </Space>
      )
    }
  }
  return {
    getColumns,
    tableColumns
  }
}
